﻿namespace EFS.RC.CC.Config.Scheduler
{
	public class Time
	{
		/// Hour
		private System.Int32 hour = 0;

		/// Minuteday
		public System.Int32 minute = 0;

		/// Minuteday
		public System.Int32 second = 0;

		/// <summary>
		/// Time
		/// </summary>
		public Time()
		{
			hour = 0;
			minute = 0;
		}

		/// Hour of the day: Get, Set
		public System.Int32 Hour
		{
			get { return hour; }
			set 
			{
				EFS.Common.Verify.Condition(value >= 0 && value <= 23, "hour >= 0 && hour <= 23");
				hour = value; 
			}
		}

		/// <summary>
		/// get date time 
		/// </summary>
		public System.DateTime DateTime
		{
			get 
			{
				System.DateTime today = System.DateTime.Now.Date;
				return new System.DateTime(today.Year, today.Month, today.Day, hour, minute, second);
			}
		}

		/// Minute of the hour: Get, Set
		public System.Int32 Minute
		{
			get { return minute; }
			set
			{
				EFS.Common.Verify.Condition(value >= 0 && value <= 59, "minute >= 0 && minute <= 59");
				minute = value;
			}
		}

		/// Second of the hour: Get, Set
		public System.Int32 Second
		{
			get { return second; }
			set
			{
				EFS.Common.Verify.Condition(value >= 0 && value <= 59, "second >= 0 && second <= 59");
				second = value;
			}
		}

		/// Serialize
		public void Serialize(System.String tagName, EFS.Common.Serialization.XmlWriter xmlWriter)
		{
			// write
			xmlWriter.Write(tagName, System.String.Format("{0}:{1}:{2}", Hour, Minute, Second));
		}

		/// Deserialize
		public void Deserialize(System.String tagName, EFS.Common.Serialization.XmlReader xmlReader)
		{
			// read
			System.String time = xmlReader.Read(tagName);
			System.String[] hms = time.Split(new System.Char[] { ':' });
			if (hms.Length > 0)
			{
				Hour = System.Int32.Parse(hms[0]);
				if (hms.Length > 1)
				{
					Minute = System.Int32.Parse(hms[1]);
					if (hms.Length > 2)
					{
						Second = System.Int32.Parse(hms[2]);
					}
				}
			}
		}
	}
}